package chapter1.zyq2_senFile;

import java.net.ServerSocket;
import java.net.Socket;


//需要完成SendReceiveUtil_File5的longToByteArray()、sendFile()、byteArrayToLong()、receiveFile
//需要完成server端和Client端的communicate()
public class Server5 {
    public static void main(String[] args) throws Exception {
        Server5 server = new Server5();
        server.communicate();
    }

    private void communicate() throws Exception {
        ServerSocket serverSocket = new ServerSocket(8001);
        System.out.println("服务器开启服务端口" + 8001 + ",等待连接请求...");
        Socket socket = serverSocket.accept();
        System.out.println("接受客户端的连接，开始会话...");
        //设置服务端socket读超时为20000毫秒
        socket.setSoTimeout(1000);

        Thread.sleep(900);
        String fileName=SendReceiveUtil_File5.receiveString(socket);
        System.out.println("接收文件名: " + fileName);
        String filePath="D:\\receiveFromServer-" + fileName;

        Thread.sleep(900);
        System.out.println("接收并保存文件: " + filePath);
        SendReceiveUtil_File5.receiveFile(socket, filePath);


        Thread.sleep(900);
        filePath="D:\\w.png";
        fileName=filePath.substring(filePath.lastIndexOf("\\")+1);
        System.out.println("发送文件名: " + fileName);

        Thread.sleep(900);
        SendReceiveUtil_File5.sendString(socket,fileName);
        System.out.println("发送文件: " + filePath);
        SendReceiveUtil_File5.sendFile(socket, filePath);

        try{
            socket.close();
            System.out.println("会话结束，服务端关闭连接。");
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
